package node;

import java.util.Arrays;

public class FourNode<T> {
    private FourNode<T> up;
    private FourNode<T> down;
    private FourNode<T> left;
    private FourNode<T> right;
    private T data;

    public FourNode<T> getUp() {
        return up;
    }

    public void setUp(FourNode<T> up) {
        this.up = up;
    }

    public FourNode<T> getDown() {
        return down;
    }

    public void setDown(FourNode<T> down) {
        this.down = down;
    }

    public FourNode<T> getLeft() {
        return left;
    }

    public void setLeft(FourNode<T> left) {
        this.left = left;
    }

    public FourNode<T> getRight() {
        return right;
    }

    public void setRight(FourNode<T> right) {
        this.right = right;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String toString(){
        return data+"："+(up!=null?" 上"+up.getData():"")+
                (left!=null?" 左"+left.getData():"")+
                (right!=null?" 右"+right.getData():"")+
                (down!=null?" 下"+down.getData():"");
    }

    //生成n*n的矩阵
    public static void createMatrix(int n){
        System.out.println("n="+n);
        System.out.println();

        FourNode<Integer>[][] nodes=new FourNode[n][n];
        for (int i=0;i<nodes.length;i++){
            FourNode<Integer>[] line=nodes[i];
            for (int j=0;j<line.length;j++){
                FourNode<Integer> node=new FourNode<Integer>();
                node.setData(i*n+(j+1));
                nodes[i][j]=node;

                if (i>0){
                    FourNode<Integer> up=nodes[i-1][j];
                    node.setUp(up);
                    up.setDown(node);
                }
                if (j>0){
                    FourNode<Integer> left=nodes[i][j-1];
                    node.setLeft(left);
                    left.setRight(node);
                }
                /*
                if (i<n-1){
                    node.FourNode<Integer> down=nodes[i+1][j];
                    node.setDown(down);
                }
                if (j<n-1){
                    node.FourNode<Integer> right=nodes[i][j+1];
                    node.setRight(right);
                }*/

                System.out.print(node.getData()+" ");
            }
            System.out.println();
        }

        System.out.println();


        Arrays.stream(nodes).forEach(line->{
            Arrays.stream(line).forEach(node->{
                System.out.print(node.toString()+" ");
            });
            System.out.println();
        });

        for (int i=0;i<nodes.length;i++){
            FourNode<Integer>[] line=nodes[i];
            for (int j=0;j<line.length;j++){
                //node.FourNode<Integer> node=nodes[i][j];
                //System.out.print(node.toString()+"，");
            }
            System.out.println();
        }
    }
}
